using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;
using Vector2 = UnityEngine.Vector2;

public class UIGoldProgress : MonoBehaviour
{
    public RectTransform rect;

    public Transform BG;

    public GameObject ImageParent;

    public Image progress;

    public List<Sprite> numberSprites;

    public Image Number1;

    public Image Number2;

    public Image Number3;

    public Image GoldPrefab;

    public Image TargetGoldImage; //目标点

    private List<Image> GoldPrefabLists = new List<Image>();
    // Start is called before the first frame update

    private void Awake()
    {
        rect = GetComponent<RectTransform>();
    }

    private void Start()
    {
        if (Screen.width > Screen.height)
        {
            BG.transform.localScale = new Vector3(3, 3, 3);
        }
        else
        {
            BG.transform.localScale = new Vector3(1, 1, 1);
        }
        
        EventManager.Instance.Subscribe(ScreenChangeEvent.EventId,ScreenChangeCallBack);
    }


    public async void GetGold(GameObject go)
    {
        Image goldImage = null;
        if (GoldPrefabLists.Count != 0)
        {
            goldImage = GoldPrefabLists[0];
            GoldPrefabLists.RemoveAt(0);
        }
        else
        {
            goldImage = Instantiate(GoldPrefab).GetComponent<Image>();
            goldImage.transform.SetParent(ImageParent.transform);
            goldImage.transform.localScale = Vector3.one;
        }

        goldImage.gameObject.SetActive(true);
        Vector2 targetpos = UpdateRectPosition(go.transform.position, Vector2.zero);
        goldImage.rectTransform.anchoredPosition = targetpos;
        goldImage.rectTransform.sizeDelta = new Vector2(139, 119);

        goldImage.rectTransform.anchoredPosition = UpdateRectPosition(go.transform.position, Vector2.zero);
        
        // goldImage.rectTransform.anchorMax = new Vector2(0.5f, 1f);
        // goldImage.rectTransform.anchorMin = new Vector2(0.5f, 1f);

        Vector3 targetWorldPos = TargetGoldImage.rectTransform.TransformPoint(TargetGoldImage.rectTransform.rect.center);
        goldImage.rectTransform.DOAnchorPos(goldImage.rectTransform.parent.InverseTransformPoint(targetWorldPos), 0.5f).SetEase(Ease.OutBack).OnComplete(
            () =>
            {
                goldImage.gameObject.SetActive(false);
                GoldPrefabLists.Add(goldImage);
                GameEntry.Instance.score += 1;
                GameEntry.Instance.UIGoldProgress.RefreshProgress();
            });
        
        // goldImage.rectTransform.DOAnchorPos(TargetGoldImage.rectTransform.anchoredPosition, 0.5f).SetEase(Ease.OutBack).OnComplete(
        //     () =>
        //     {
        //         goldImage.gameObject.SetActive(false);
        //         GoldPrefabLists.Add(goldImage);
        //         GameEntry.Instance.score += 1;
        //         GameEntry.Instance.UIGoldProgress.RefreshProgress();
        //     });
    }

    public void SetGold(Vector3 targetposition,Action CompleteAction)
    {
        Image goldImage = null;
        if (GoldPrefabLists.Count != 0)
        {
            goldImage = GoldPrefabLists[0];
            GoldPrefabLists.RemoveAt(0);
        }
        else
        {
            goldImage = Instantiate(GoldPrefab).GetComponent<Image>();
            goldImage.transform.SetParent(ImageParent.transform);
            goldImage.transform.localScale = Vector3.one;
            goldImage.rectTransform.anchoredPosition = TargetGoldImage.rectTransform.anchoredPosition;
        }

        Debug.LogError(33333);
        
        goldImage.gameObject.SetActive(true);
        Vector2 targetpos = UpdateRectPosition(targetposition, Vector2.zero);
        Vector3 targetposWorld = Camera.main.ScreenToWorldPoint(targetpos);
        goldImage.rectTransform.sizeDelta = new Vector2(139, 119);
        // goldImage.rectTransform.anchorMax = new Vector2(0.5f, 1f);
        // goldImage.rectTransform.anchorMin = new Vector2(0.5f, 1f);
        
        goldImage.rectTransform.DOMove(targetposWorld, 0.5f).SetEase(Ease.OutBack).OnComplete(
            () =>
            {
                goldImage.gameObject.SetActive(false);
                GoldPrefabLists.Add(goldImage);
                CompleteAction.Invoke();
            });
    }
    public void RefreshProgress()
    {
        Number1.gameObject.SetActive(false);
        Number2.gameObject.SetActive(false);
        Number3.gameObject.SetActive(false);

        int score = GameEntry.Instance.score;
        progress.fillAmount = (float)score / 999;
        int score1 = score / 100;
        int score2 = (score % 100) / 10;
        int score3 = score % 10;
        if (score >= 999)
        {
            Number1.gameObject.SetActive(true);
            Number2.gameObject.SetActive(true);
            Number3.gameObject.SetActive(true);
            Number1.sprite = numberSprites[9];
            Number1.rectTransform.sizeDelta = new Vector2(numberSprites[9].texture.width,numberSprites[9].texture.height);

            Number2.sprite = numberSprites[9];
            Number2.rectTransform.sizeDelta = new Vector2(numberSprites[9].texture.width,numberSprites[9].texture.height);

            Number3.sprite = numberSprites[9];
            Number3.rectTransform.sizeDelta = new Vector2(numberSprites[9].texture.width,numberSprites[9].texture.height);
        }
        else
        {
            if (score1 != 0)
            {
                Number1.gameObject.SetActive(true);
                Number1.sprite = numberSprites[score1];
                Number1.rectTransform.sizeDelta = new Vector2(numberSprites[score1].texture.width,numberSprites[score1].texture.height);
            }

            if (score1 != 0)
            {
                Number2.gameObject.SetActive(true);
                Number2.sprite = numberSprites[score2];
                Number2.rectTransform.sizeDelta = new Vector2(numberSprites[score2].texture.width,numberSprites[score2].texture.height);
            }
            else if(score2 != 0)
            {
                Number2.gameObject.SetActive(true);
                Number2.sprite = numberSprites[score2];
                Number2.rectTransform.sizeDelta = new Vector2(numberSprites[score2].texture.width,numberSprites[score2].texture.height);
            }

            Number3.gameObject.SetActive(true);
            Number3.sprite = numberSprites[score3];
            Number3.rectTransform.sizeDelta = new Vector2(numberSprites[score3].texture.width,numberSprites[score3].texture.height);
        }

    }

    private Vector2 UpdateRectPosition(Vector3 EnemyTransform,Vector2 Offset)
    {
        Vector3 MyWorldPosition =EnemyTransform;
        Vector2 screenPosition = Camera.main.WorldToScreenPoint(MyWorldPosition);
        Vector2 localPosition;
        if (RectTransformUtility.ScreenPointToLocalPointInRectangle(rect,screenPosition,null,out localPosition))
        {
            return localPosition + Offset;
        }
        return Vector2.zero;
    }

    private void ScreenChangeCallBack(object sender,EventArgs ea)
    {
        if (Screen.width > Screen.height)
        {
            BG.transform.localScale = new Vector3(3, 3, 3);
        }
        else
        {
            BG.transform.localScale = new Vector3(1, 1, 1);
        }
    }
}
